/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var searchRange = function (nums, target) {

  let left = search(nums, target, true)
  let right = search(nums, target, false)
  
  function search(nums, target, lower) {
    let n = nums.length
    let l = 0,
      r = n - 1
    while (l <= r) {
      let mid = Math.floor((r + l) / 2)
      if (nums[mid] > target) {
        r = mid - 1
      } else if (nums[mid] < target) {
        l = mid + 1
      } else {
        if (lower) {
          if (mid == 0 || nums[mid - 1] != target) {
            return mid;
          } else {
            r = mid - 1;
          }
        } else {
          if (mid === n - 1 || nums[mid + 1] !== target) {
            return mid
          } else {
            l = mid + 1
          }
        }


      }
    }
    return -1
  }


  return [left,right]
};

var nums = [5, 7, 7, 8, 8, 10],
  target = 8
console.log(searchRange(nums, target))
